Etienne Ackermann, 07/05/2015
Demonstrates how to draw a filled (hatched) polygon, as well as how to use the interactive widgets in Jupyter.
In [1]:
# specify the N-arm radial maze parameters:
num_arms = 8;
arm_length = 50; # cm
base_radius = 15; # cm
In [2]:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Polygon
%matplotlib inline
from IPython.html.widgets import interactive
from IPython.display import display
def draw_radial_maze(num_arms=8, base_radius=15.0, arm_length=50.0):
# determine track width:
tw = 2*np.pi*base_radius/num_arms
theta_array = np.linspace(0, 2*np.pi,num_arms+1)
#thetavec = thetavec(1:end-1):
theta_array = np.delete(theta_array,-1)
rectangle_vertices = np.array([[base_radius*np.cos(theta_array[1]/2),tw/2], [arm_length+base_radius, tw/2],[arm_length+base_radius, -tw/2]])
maze_polygon = np.zeros((num_arms*3,2))
for arm in np.arange(num_arms):
theta = theta_array[arm]
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta)],[np.sin(theta), np.cos(theta)]])
maze_polygon[(arm)*3:(arm+1)*3,:] = np.dot(rectangle_vertices,rotation_matrix)
patterns = ('-', '+', 'x', '\\', '*', 'o', 'O', '.')
fig, ax = plt.subplots()
ax.add_patch(Polygon(maze_polygon, closed=True, fill=False, hatch='O'))
ax.set_xlim((np.floor(-1.1*(base_radius+arm_length)), np.ceil(1.1*(base_radius+arm_length))))
ax.set_ylim((-1.1*(base_radius+arm_length)), np.ceil(1.1*(base_radius+arm_length)))
ax.set_aspect('equal', adjustable='box')
# draw scale bar:
#line([(base_radius+arm_length)/2, (base_radius+arm_length)], [-0.9*(base_radius+arm_length), -0.9*(base_radius+arm_length)],'Color','k','LineWidth',2);
#line([(base_radius+arm_length)/2, (base_radius+arm_length)/2], [-0.9*(base_radius+arm_length)-0.05*(base_radius+arm_length), -0.9*(base_radius+arm_length)+0.05*(base_radius+arm_length)],'Color','k','LineWidth',2);
#line([(base_radius+arm_length), (base_radius+arm_length)], [-0.9*(base_radius+arm_length)-0.05*(base_radius+arm_length), -0.9*(base_radius+arm_length)+0.05*(base_radius+arm_length)],'Color','k','LineWidth',2);
#text((base_radius+arm_length)/2 + 0.05*(base_radius+arm_length), -0.9*(base_radius+arm_length)-0.08*(base_radius+arm_length), sprintf('%g cm',(base_radius+arm_length)/2));
In [3]:
v = interactive(draw_radial_maze, num_arms=(2,30), base_radius=(1.0,30.0), arm_length=(1.0,150.0))
display(v)
In [ ]:
from IPython.core.display import HTML
def css_styling():
styles = open("../styles/custom.css", "r").read()
return HTML(styles)
css_styling()